import axios from './http'
import qs from 'qs'

let BASE_PATH = process.env.API_PATH

export function post(url, params = {}, needQs = 1) {
  url = BASE_PATH + url
  console.log('这里是post的token', localStorage.getItem('token'))
  
  if (localStorage.getItem('token')) {
    if (params instanceof FormData) {
      params.append('token', localStorage.getItem('token'))
    }else{
      params.token = localStorage.getItem('token')
    }
  }

  if (needQs) {
    params = qs.stringify(params)
  }

  return axios.post(url, params).then((res) => {
    return Promise.resolve(res)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

export function get(url, params = {}, needQs = 1) {
  url = BASE_PATH + url
  if (localStorage.getItem('token')) {
    params.token = localStorage.getItem('token')
  }
  if (needQs && params) {
    url = url + '?' + qs.stringify(params)
  }
  return axios.get(url).then((res) => {
    return Promise.resolve(res)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
